home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 1.toast / pc / what's new? / sample code / human interface toolbox / auntiedialog / auntiedialogdemo.c < prev    next >
Encoding:
Text File  |  2000-06-23  |  9.2 KB  |  384 lines

  1. /*
  2.     File            :    AuntieDialogDemo.c
  3.     
  4.     Description        :    Free of the Dialog Manager at last! See: "AuntieDialog.html"
  5.  
  6.     Author            :    PCG
  7.  
  8.     Copyright        :    © Copyright 1999-2000 Apple Computer, Inc. All rights reserved.
  9.     
  10.     Disclaimer        :    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  11.                         ("Apple") in consideration of your agreement to the following terms, and your
  12.                         use, installation, modification or redistribution of this Apple software
  13.                         constitutes acceptance of these terms.  If you do not agree with these terms,
  14.                         please do not use, install, modify or redistribute this Apple software.
  15.  
  16.                         In consideration of your agreement to abide by the following terms, and subject
  17.                         to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  18.                         copyrights in this original Apple software (the "Apple Software"), to use,
  19.                         reproduce, modify and redistribute the Apple Software, with or without
  20.                         modifications, in source and/or binary forms; provided that if you redistribute
  21.                         the Apple Software in its entirety and without modifications, you must retain
  22.                         this notice and the following text and disclaimers in all such redistributions of
  23.                         the Apple Software.  Neither the name, trademarks, service marks or logos of
  24.                         Apple Computer, Inc. may be used to endorse or promote products derived from the
  25.                         Apple Software without specific prior written permission from Apple.  Except as
  26.                         expressly stated in this notice, no other rights or licenses, express or implied,
  27.                         are granted by Apple herein, including but not limited to any patent rights that
  28.                         may be infringed by your derivative works or by other works in which the Apple
  29.                         Software may be incorporated.
  30.  
  31.                         The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  32.                         WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  33.                         WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  34.                         PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  35.                         COMBINATION WITH YOUR PRODUCTS.
  36.  
  37.                         IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  38.                         CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  39.                         GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  40.                         ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  41.                         OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  42.                         (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  43.                         ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.            
  44. */
  45.  
  46. #import "AuntieDialog.h"
  47. #import "AppearanceHelpers.h"
  48.  
  49. #import <Sound.h>
  50. #import <TextUtils.h>
  51.  
  52. enum
  53. {
  54.     kResID_Dialog_Mega                        = 6000,
  55.     kResID_DialogItemList_AllowChangeFocus    = 8000,
  56.     kResID_Dialog_NoChangeFocus                = 9000
  57. };
  58.  
  59. enum
  60. {
  61.     kControlValue_Tabs_Classic = 1,
  62.     kControlValue_Tabs_Sliders,
  63.     kControlValue_Tabs_Text,
  64.     kControlValue_Tabs_Progress,
  65.     kControlValue_Tabs_Groups,
  66.     kControlValue_Tabs_GrabBag
  67. };
  68.  
  69. static             Boolean        gQuitting;
  70. static const    SInt16        gResourceIDsForTabs [ ] = { 6003, 6002, 6004, 6001, 6005, 6006 };
  71.  
  72. static OSStatus PopulateListBox (WindowPtr window)
  73. {
  74.     OSStatus err = noErr;
  75.  
  76.     do
  77.     {
  78.         ListHandle    list;
  79.         ControlRef    listControl;
  80.         Cell        cell;
  81.         SInt16        i;
  82.         Str255        string;
  83.         ControlID    controlID = { kAuntieDialogSignature, 8 };
  84.  
  85.         err = GetControlByID (window,&controlID,&listControl);
  86.         if (err) break;
  87.         err = GetListBoxListHandle (listControl, &list);
  88.         if (err) break;
  89.         
  90.         cell.h = 0;
  91.         
  92.         for ( i = 1; true; i++ )
  93.         {
  94.             GetIndString( string, 130, i );
  95.             if ( string[0] == 0 ) break;
  96.         
  97.             LAddRow( 1, (**list).dataBounds.bottom, list );
  98.             cell.v = (SInt16) ((**list).dataBounds.bottom - 1);
  99.             LSetCell( (Ptr)(string + 1), string[0], cell, list );
  100.         }
  101.     }
  102.     while (false);
  103.  
  104.     return err;
  105. }
  106.  
  107. static pascal OSStatus SetUpClassicTab (WindowRef window)
  108. {
  109.     OSStatus err = noErr;
  110.  
  111.     do
  112.     {
  113.         ControlID    controlID = { kAuntieDialogSignature, 3 };
  114.         ControlRef    control;
  115.  
  116.         err = GetControlByID (window,&controlID,&control);
  117.         if (err) break;
  118.         err = SetDefaultControl (control,true);
  119.         if (err) break;
  120.         err = InvalControl (control,false);
  121.         if (err) break;
  122.     }
  123.     while (false);
  124.  
  125.     return err;
  126. }
  127.  
  128. static pascal OSStatus SetUpNewTab (ControlRef itemHit)
  129. {
  130.     OSStatus err = noErr;
  131.  
  132.     err = DisposeChildControls (itemHit);
  133.  
  134.     if (!err)
  135.     {
  136.         SInt16            tabIndex        = GetControlValue (itemHit);
  137.         SInt16            resID            = gResourceIDsForTabs [tabIndex - 1];
  138.         WindowRef        owningWindow    = GetControlOwner (itemHit);
  139.  
  140.         err = AppendDialogItemsAsControls (resID, owningWindow, nil, nil);
  141.  
  142.         if (!err)
  143.         {
  144.             ControlRef    control;
  145.             ControlID    controlID;
  146.  
  147.             switch (tabIndex)
  148.             {
  149.                 case kControlValue_Tabs_Classic :
  150.  
  151.                     err = SetUpClassicTab (owningWindow);
  152.                     break;
  153.  
  154.                 case kControlValue_Tabs_Text    :
  155.  
  156.                     err = PopulateListBox (owningWindow);
  157.                     if (err) break;
  158.                     err = AppendDialogItemsAsControls (kResID_DialogItemList_AllowChangeFocus, owningWindow, nil, nil);
  159.                     break;
  160.  
  161.                 case kControlValue_Tabs_Progress :
  162.  
  163.                     controlID.signature        = kAuntieDialogSignature;
  164.                     controlID.id            = 3;
  165.  
  166.                     err = GetControlByID (owningWindow,&controlID,&control);
  167.                     if (err) break;
  168.                     err = SetProgressIndicatorState (control,false);
  169.                     break;
  170.             }
  171.         }
  172.     }
  173.  
  174.     return err;
  175. }
  176.  
  177. static pascal OSStatus RespondToApplicationEvent (const EventRecord *event, RgnHandle mouseRgn)
  178. {
  179.     OSStatus err = noErr;
  180.  
  181.     if (event->what == mouseDown)
  182.     {
  183.         WindowRef    window;
  184.         Rect        rect;
  185.         BitMap        screenBits;
  186.  
  187.         switch (FindWindow (event->where, &window))
  188.         {
  189.             case inGoAway :
  190.  
  191.                 if (TrackGoAway (window, event->where))
  192.                 {
  193.                     gQuitting = true;
  194.                 }
  195.                 break;
  196.  
  197.             case inDrag :
  198.  
  199.                 GetQDGlobalsScreenBits (&screenBits);
  200.                 rect = screenBits.bounds;
  201.                 InsetRect (&rect,4,4);
  202.                 DragWindow (window,event->where,&rect);
  203.                 SetEmptyRgn (mouseRgn);
  204.                 break;
  205.         }
  206.     }
  207.  
  208.     return err;
  209. }
  210.  
  211. static pascal OSStatus FocusChangeValidatorControlCreationProc (ControlRef control, void *)
  212. {
  213.     ControlID controlID;
  214.  
  215.     OSStatus err = GetControlID (control,&controlID);
  216.  
  217.     if (!err && controlID.id == 1)
  218.     {
  219.         err = SetPushButtonDefaultState (control,true);
  220.     }
  221.  
  222.     return err;
  223. }
  224.  
  225. static pascal OSStatus FocusChangeValidator (ControlRef control, Boolean *allow)
  226. {
  227.     OSStatus err = noErr;
  228.  
  229.     do
  230.     {
  231.         WindowRef    contrlOwner                    = GetControlOwner (control);
  232.         ControlID    controlID                    = { kAuntieDialogSignature, 12 };
  233.         ControlRef    allowFocusChangeCheckBox;
  234.  
  235.         err = GetControlByID (contrlOwner,&controlID,&allowFocusChangeCheckBox);
  236.         if (err) break;
  237.         *allow = (Boolean) GetControlValue (allowFocusChangeCheckBox);
  238.  
  239.         if (!*allow)
  240.         {
  241.             WindowRef    duhWindow;
  242.             ControlRef    itemHit;
  243.  
  244.             err = GetAuntieDialog
  245.                 (kResID_Dialog_NoChangeFocus,kFirstWindowOfClass,
  246.                     FocusChangeValidatorControlCreationProc,nil,&duhWindow);  
  247.             if (err) break;
  248.             err = AuntieModalDialog (duhWindow,&itemHit,nil,nil);
  249.             DisposeWindow (duhWindow);
  250.             if (err) break;
  251.         }
  252.     }
  253.     while (false);
  254.  
  255.     return err;
  256. }
  257.  
  258. static pascal OSStatus LetAuntieDialogRespondToEvent
  259.     (RgnHandle mouseRgn, const EventRecord *event, WindowRef window)
  260. {
  261.     OSStatus err = noErr;
  262.  
  263.     do
  264.     {
  265.         ControlRef    tabsControl;
  266.         SInt16        tabsControlValueBefore;
  267.         ControlRef    itemHit;
  268.         ControlID    controlID    = { kAuntieDialogSignature, 1 };
  269.  
  270.         err = GetControlByID (window,&controlID,&tabsControl);
  271.         if (err) break;
  272.  
  273.         tabsControlValueBefore = GetControlValue (tabsControl);
  274.  
  275.         err = AuntieDialogSelect (event,mouseRgn,&itemHit,FocusChangeValidator);
  276.         if (err || !itemHit) break;
  277.  
  278.         if (itemHit == tabsControl)
  279.         {
  280.             if (tabsControlValueBefore != GetControlValue (tabsControl))
  281.             {
  282.                 err = SetUpNewTab (itemHit);
  283.                 if (err) break;
  284.             }
  285.         }
  286.         else switch (GetControlValue (tabsControl))
  287.         {
  288.             case kControlValue_Tabs_Text :
  289.  
  290.                 err = GetControlID (itemHit,&controlID);
  291.                 if (err) break;
  292.  
  293.                 if (12 == controlID.id)
  294.                 {
  295.                     SetControlValue (itemHit, !GetControlValue (itemHit));
  296.                 }
  297.  
  298.                 break;
  299.         }
  300.     }
  301.     while (false);
  302.  
  303.     return err;
  304. }
  305.  
  306. static OSStatus EventLoop (WindowRef auntieDialogWindow)
  307. {
  308.     OSStatus err = noErr;
  309.  
  310.     RgnHandle mouseRgn = NewRgn ( );
  311.  
  312.     if (!mouseRgn)
  313.     {
  314.         err = QDError ( );
  315.     }
  316.     else
  317.     {
  318.         UInt32 adIdleTime = AuntieDialogGetIdleInterval (auntieDialogWindow);
  319.  
  320.         while (!gQuitting)
  321.         {
  322.             EventRecord event;
  323.  
  324.             UInt32 sleepTime = IsWindowHilited (auntieDialogWindow) ? adIdleTime : 0xFFFFFFFF;
  325.  
  326.             (void) WaitNextEvent (everyEvent, &event, sleepTime, mouseRgn);
  327.  
  328.             do
  329.             {
  330.                 WindowRef window;
  331.                 err = IsAuntieDialogEvent (&event,&window);
  332.                 if (err) break;
  333.  
  334.                 if (!window)
  335.                 {
  336.                     err = RespondToApplicationEvent (&event,mouseRgn);
  337.                 }
  338.                 else if (window != auntieDialogWindow)
  339.                 {
  340.                     DebugStr ("\punknown window produced by IsAuntieDialogEvent");
  341.                 }
  342.                 else
  343.                 {
  344.                     err = LetAuntieDialogRespondToEvent (mouseRgn,&event,window);
  345.                 }
  346.  
  347.                 if (err) break;
  348.             }
  349.             while (false);
  350.         }
  351.         DisposeRgn (mouseRgn);
  352.     }
  353.  
  354.     return err;
  355. }
  356.  
  357. void main (void)
  358. {
  359.     OSStatus err = noErr;
  360.  
  361.     WindowRef window;
  362.  
  363.     InsertMenu (GetMenu (147), -1);
  364.  
  365.     err = GetAuntieDialog (kResID_Dialog_Mega,kFirstWindowOfClass,nil,nil,&window);
  366.  
  367.     if (!err)
  368.     {
  369.         err = AppendDialogItemsAsControls (gResourceIDsForTabs [0], window, nil, nil);
  370.  
  371.         if (!err)
  372.         {
  373.             err = SetUpClassicTab (window);
  374.  
  375.             if (!err)
  376.             {
  377.                 err = EventLoop (window);
  378.             }
  379.         }
  380.  
  381.         DisposeWindow (window);
  382.     }
  383. }
  384.